package com.mall.entity;

import lombok.Data;
import java.math.BigDecimal;

/**
 * 订单商品实体
 * 
 * @author Mall Team
 */
@Data
public class OrderItem {
    
    private Long id;
    private Long orderId;
    private Long userId;
    private Long productId;
    private String productName;
    private String productImage;
    private BigDecimal price;
    private BigDecimal totalPrice;
    private Integer quantity;
    private BigDecimal totalAmount;
    
    // 关联对象
    private Product product;
    
    /**
     * 构造函数 - 根据商品和数量创建订单项
     */
    public OrderItem(Long orderId, Product product, Integer quantity) {
        this.orderId = orderId;
        this.productId = product.getId();
        this.productName = product.getName();
        this.productImage = product.getMainImage();
        this.price = product.getPrice();
        this.quantity = quantity;
        this.totalAmount = product.getPrice().multiply(new BigDecimal(quantity));
    }
    
    public OrderItem() {}
    
    /**
     * 计算小计
     */
    public BigDecimal calculateTotalAmount() {
        if (price != null && quantity != null) {
            this.totalAmount = price.multiply(new BigDecimal(quantity));
            return this.totalAmount;
        }
        return BigDecimal.ZERO;
    }
}